package com.platform.framework.controller.rights;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.platform.common.utils.JsonUtils;
import com.platform.common.utils.PageUtils;
import com.platform.framework.controller.BaseController;
import com.platform.framework.entity.rights.FwRightRole;
import com.platform.framework.service.rights.FwRightRoleService;

@RestController
@RequestMapping("/role")
public class FwRightRoleController extends BaseController {

	@Autowired
	FwRightRoleService fwRightRoleService;

	/**
	 * 
	 * roleIndex主页地址
	 * 
	 * @return
	 */
	@RequestMapping("/roleIndex")
	public ModelAndView welcome(Model model) {
		ModelAndView mv = new ModelAndView("user/roleIndex");
		return mv;
	}
	
	@PreAuthorize("hasAuthority('role')")//必须要有admin权限的才能访问  
    @RequestMapping("/index")  
    public ModelAndView authorize() {  
    	ModelAndView mv = new ModelAndView("rights/role/index");
        return mv;  
    }  
	
	/**
	 * 
	  * 查询所有的数据	
	  * @param request
	  * @return role分页语句
	  * @throws Exception
	  * @createDate 2017-7-10 上午9:28:17
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "pagelist", method = { RequestMethod.POST })
	public String rolePageList(HttpServletRequest request) throws Exception {
		String searchCtx = request.getParameter("searchPhrase");//查询条件框
		Pageable pageable = null;
		try{
			pageable = PageUtils.dealminiRequest(request);
		}catch (Exception e) {
			e.printStackTrace();
		}
		Page<FwRightRole> pages= null;
		pages= fwRightRoleService.findRolePage(searchCtx,pageable);
		return JsonUtils.dealMiniPage(pages);
	}
	
	
	/**
	 * 
	  * 根据roleId查询对应的功能模块 	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param request
	  * @return
	 * @throws Exception 
	  * @createDate 2017-7-24 上午10:00:10
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@PreAuthorize("hasAuthority('role:toModule')")
	@RequestMapping(value = "bandlemoudles", method = { RequestMethod.POST })
	public String bandlemoudles(HttpServletRequest request) throws Exception{
		String roleId = request.getParameter("roleId");
		String modules = request.getParameter("modules");
		return fwRightRoleService.saveModuleIdsByRoleId(roleId, modules);
	}
	
	
	/**
	 * 
	  * 根据roleId查询对应的功能模块 	
	  * @author 王求卿[wangqiuqing] Email:455654995@qq.com Tel:18101037995
	  * @param request
	  * @return
	 * @throws Exception 
	  * @createDate 2017-7-24 上午10:00:10
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "moudles", method = { RequestMethod.GET })
	public String moudles(HttpServletRequest request) throws Exception{
		String roleId = request.getParameter("roleId");
		String modules = fwRightRoleService.findModuleIdsByRoleId(roleId);
		return modules;
	}

	/**
	 * 
	  * 	
	  * @author 王锋[wangfeng] Email:455654995@qq.com Tel:18101037995
	  * @return
	  * @throws Exception
	  * @createDate 2017-7-20 上午9:55:21
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@RequestMapping(value = "rolelist", method = { RequestMethod.GET })
	public String roleList() throws Exception {
		List<FwRightRole> lists= fwRightRoleService.findRoles();
		return JsonUtils.dealJson(lists);
	}
	
	/**
	 * 
	  * 角色添加，修改页面界面 	
	  * @return
	  * @createDate 2017-7-13 上午9:37:43
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
    @RequestMapping("/edit")  
    public ModelAndView orgAdd(){
    	ModelAndView mv = new ModelAndView("rights/role/edit");
		return mv;
    }
    
    /**
	 * 
	  * 添加 	
	  * @return
	  * @createDate 2017-7-13 上午9:36:57
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@PreAuthorize("hasAuthority('role:add')")
	@RequestMapping(value = "add", method = { RequestMethod.POST })
	public String orgSave(HttpServletRequest request){
		String data = request.getParameter("data");
		FwRightRole fwRightRole =  JSONObject.parseObject(data, FwRightRole.class);
		fwRightRole.setId(this.getUUID());
		fwRightRole.setCreateTime(new Date());
		fwRightRole.setCreateUserCode(this.getUserCode());//操作用户
		fwRightRole.setCreateUserName(this.getUserEntity().getUsername());//操作编码
		fwRightRole.setIsDeleted("0");//未删除
		fwRightRole.setIsEnable("1");//可用
		return fwRightRoleService.save(fwRightRole);
	}
	/**
	 * 
	 * 添加权限	
	 * @return
	 * @createDate 2017-7-13 上午9:36:57
	 * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	 
    @RequestMapping("/addModule")  
    public ModelAndView addModule(){
    	ModelAndView mv = new ModelAndView("rights/role/rolemodule");
		return mv;
    }
	
	/**
	 * 
	  * 更新	
	  * @return
	  * @createDate 2017-7-13 上午9:36:57
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@PreAuthorize("hasAuthority('role:update')")
	@RequestMapping(value = "update", method = { RequestMethod.POST })
	public String orgUpdate(HttpServletRequest request){
		String data = request.getParameter("data");
		FwRightRole fw =  JSONObject.parseObject(data, FwRightRole.class);
		fw.setUpdateTime(new Date());
		fw.setUpdateUserCode(this.getUserCode());
		fw.setUpdateUserName(this.getUserEntity().getUsername());
		fw.setIsDeleted("0");
		return fwRightRoleService.update(fw);
	}
	
	/**
	 * 
	  * 删除
	  * @return
	  * @createDate 2017-7-13 上午9:36:57
	  * @update:[日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	@PreAuthorize("hasAuthority('role:delete')")
	@RequestMapping(value = "delete", method = { RequestMethod.DELETE })
	public String orgDelete(HttpServletRequest request){
		String ids = request.getParameter("ids");
		return fwRightRoleService.delete(ids);
	}
	
	
    

}